/**
 * @file bit_swap.c
 * @brief 蝶式交换
 * @author yejiahao
 * @date 2023-09-07 01:02:56
 * 
 * @copyright Copyright (c) 2023 by 深圳市鹏天微智能科技有限公司, All Rights Reserved.
 */
#include <stdio.h>
#include <stdint.h>

/*********************
 *  蝶式交换/高低位交换
 *********************/
/* 
碟式交换数据变化过程：
    原始数据：  1 2 3 4 5 6 7 8
    第一步：    5 6 7 8 1 2 3 4
    第二步：    7 8 5 6 3 4 1 2
    第三步：    8 7 6 5 4 3 2 1
 */
uint8_t high_and_low_shift8(uint8_t data)  
{  
    data = (data << 4) | (data >> 4);
    data = ((data << 2) & 0xcc) | ((data >> 2) & 0x33);
    data = ((data << 1) & 0xaa) | ((data >> 1) & 0x55);
    return data;    
}

uint16_t high_and_low_shift16(uint16_t data)  
{  
    data = (data << 8) | (data >> 8);
    data = ((data << 4) & 0xf0f0) | ((data >> 4) & 0x0f0f);
    data = ((data << 2) & 0xcccc) | ((data >> 2) & 0x3333);
    data = ((data << 1) & 0xaaaa) | ((data >> 1) & 0x5555);
    return data;
}

uint32_t high_and_low_shift32(uint32_t data)  
{
    data = (data << 16) | (data >> 16);
    data = ((data << 8) & 0xff00ff00) | ((data >> 8) & 0x00ff00ff);
    data = ((data << 4) & 0xf0f0f0f0) | ((data >> 4) & 0x0f0f0f0f);
    data = ((data << 2) & 0xcccccccc) | ((data >> 2) & 0x33333333);
    data = ((data << 1) & 0xaaaaaaaa) | ((data >> 1) & 0x55555555);
    return data;
}

